package com.shgbit.sdis.eurekaservice;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {

	public static void main(String[] args) {
		loadConfig();
		SpringApplication.run(EurekaServiceApplication.class, args);
	}
	
	/**
	 * 加载配置文件
	 * <p>Title: loadConfig</p>  
	 * <p>Description: </p>
	 */
	private static void loadConfig(){
		 File fileProperties = new File(System.getProperty("user.dir")+File.separator+"config"+File.separator+"application.properties");
		 if( !fileProperties.exists()){
		    	File filePath=new File(fileProperties.getParent());
				if( !filePath.isDirectory() ){ 
						filePath.mkdirs();
				}
				try {
						fileProperties.createNewFile();
						inputStreamToFile(EurekaServiceApplication.class.getClassLoader().getResourceAsStream("application.properties"),fileProperties);
				} catch (IOException e) {
						e.printStackTrace();
				}
	    }
	}
	
	/**
	 * 将文件流转换成文件
	 * <p>Title: inputstreamtofile</p>  
	 * <p>Description: </p>  
	 * @param ins
	 * @param file
	 * @throws IOException
	 */
	private static void inputStreamToFile(InputStream ins,File file) throws IOException{
	    OutputStream os = new FileOutputStream(file);
	    int bytesRead = 0;
	    byte[] buffer = new byte[8192];
	    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
	        	os.write(buffer, 0, bytesRead);
	    }
	    os.close();
	    ins.close();
	}
}
